package com.sparrow.source.service.schedule;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.sparrow.source.persistense.UnvDevice;
import com.sparrow.source.service.IUnvDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author bryceyung
 * @since 2024/12/11
 */
@Component
@EnableScheduling
@Slf4j
public class DeviceSchedule {

    @Autowired
    private IUnvDeviceService deviceService;

    /**
     * 每2min执行一次心跳状态检测
     */
    @Scheduled(cron = "0 0/2 * * * *")
    public void runOnlineStatusCheck() {
        log.info("[runOnlineStatusCheck] 定时任务执行 ");
        List<UnvDevice> offLineList = deviceService.queryOffLineDevices();
        if (CollectionUtils.isEmpty(offLineList)) {
            return;
        }
        deviceService.saveOrUpdateBatch(offLineList.stream().peek(data -> data.setOnlineStatus(false)).collect(Collectors.toList()));
    }

}
